package com.sunfield.windpower.decoration;

import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

/**
 * Created by wxl on 2017/2/6.
 */

public class SpaceDecoration extends RecyclerView.ItemDecoration {
    public static final int TYPE_START = 1;
    public static final int TYPE_END = 2;
    public static final int TYPE_BOTH = 3;
    public static final int TYPE_MIDDLE = 4;

    int type;
    int spaceWidth;

    public SpaceDecoration(int spaceWidth) {
        this(spaceWidth, TYPE_MIDDLE);
    }

    public SpaceDecoration(int spaceWidth, int type) {
        this.spaceWidth = spaceWidth;
        this.type = type;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        boolean isVertical = true;
        int span = 1;
        if (layoutManager instanceof GridLayoutManager) {
            span = ((GridLayoutManager) layoutManager).getSpanCount();
            isVertical = ((GridLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.VERTICAL;
        } else if (layoutManager instanceof LinearLayoutManager) {
            isVertical = ((LinearLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.VERTICAL;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            span = ((GridLayoutManager) layoutManager).getSpanCount();
            isVertical = ((StaggeredGridLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.VERTICAL;
        }
        switch (type) {
            case TYPE_START:
                if (isVertical) {
                    outRect.top = spaceWidth;
                } else {
                    outRect.left = spaceWidth;
                }
                break;
            case TYPE_END:
                if (isVertical) {
                    outRect.bottom = spaceWidth;
                } else {
                    outRect.right = spaceWidth;
                }
                break;
            case TYPE_BOTH:
                if (isVertical) {
                    outRect.top = spaceWidth / 2;
                    outRect.bottom = spaceWidth / 2;
                } else {
                    outRect.left = spaceWidth / 2;
                    outRect.right = spaceWidth / 2;
                }
                break;
            case TYPE_MIDDLE:
                int index = parent.getChildAdapterPosition(view);
                if(index >= span){
                    if (isVertical) {
                        outRect.top = spaceWidth;
                    } else {
                        outRect.left = spaceWidth;
                    }
                }else{
                    outRect.set(0, 0, 0, 0);
                }
                break;
        }
    }
}
